package com.skzh.drawtask.service.impl;

import com.skzh.common.core.domain.model.LoginUser;
import com.skzh.common.utils.DateUtils;
import com.skzh.common.utils.SecurityUtils;
import com.skzh.drawtask.domain.TaskLine;
import com.skzh.drawtask.domain.TaskLinePoints;
import com.skzh.drawtask.mapper.LineMapper;
import com.skzh.drawtask.service.ILineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zr
 * @Description
 * @Date 2025/3/31 13:35
 */
@Service
public class LineServiceImpl implements ILineService {

    @Autowired
    private LineMapper lineMapper;

    /**
     * 新增线任务
     */
    @Override
    @Transactional
    public int insertTaskLine(TaskLine taskLine) {
        //1.插入线任务主体
        TaskLine taskLineMain = new TaskLine();
        taskLineMain.setLineLength(taskLine.getLineLength());
        taskLineMain.setLineName(taskLine.getLineName());
        taskLineMain.setLineColor(taskLine.getLineColor());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        taskLineMain.setCreateBy(username);
        taskLineMain.setCreateTime(DateUtils.getNowDate());

        //2.插入并获取生成的主键ID
        lineMapper.insertTaskLine(taskLineMain);
        Long lineId = taskLineMain.getId();

        //3.批量插入线顶点数据
        if (taskLine.getPoints() != null && !taskLine.getPoints().isEmpty()) {
            List<TaskLinePoints> TaskLinePointsList = taskLine.getPoints().stream()
                    .map(LinePoints -> {
                        TaskLinePoints taskLinePoints = new TaskLinePoints();
                        taskLinePoints.setLineId(lineId);
                        taskLinePoints.setXLongitude(LinePoints.getXLongitude());
                        taskLinePoints.setXLatitude(LinePoints.getXLatitude());
                        taskLinePoints.setXOrder(LinePoints.getXOrder());
                        taskLinePoints.setCreateBy(username);
                        taskLinePoints.setCreateTime(DateUtils.getNowDate());
                        return taskLinePoints;
                    }).collect(Collectors.toList());

            lineMapper.batchInsertTaskLinePoints(TaskLinePointsList);
        }

        return 1;
    }

    /**
     * 根据ID删除线任务
     * 根据ID批量删除对应线的顶点数据
     */
    @Override
    @Transactional
    public int deleteTaskLineById(Long id) {
        if (id != null) {
            //根据ID批量删除对应线的顶点数据
            lineMapper.deleteTaskLinePointsByLineId(id);
            lineMapper.deleteTaskLineById(id);
        }
        return 1;
    }

    /**
     * 根据ID批量删除线任务
     *
     **/
    @Override
    @Transactional
    public int deleteTaskLineByIds(List<Long> ids) {
        if (!ids.isEmpty()) {
            //根据ID批量删除对应线的数据
            lineMapper.deleteTaskLineByIds(ids);
            //根据ID批量删除对应线的顶点数据
            ids.forEach(id -> lineMapper.deleteTaskLinePointsByLineId(id));
        }
        return 1;
    }

    /**
     * 查询线任务
     */
    @Override
    public List<TaskLine> selectTaskLineList(TaskLine taskLine) {
        return lineMapper.selectTaskLineList(taskLine);
    }

    /**
     * 根据ID批量查询对应线的顶点信息
     * *
     */
    @Override
    public List<TaskLinePoints> selectTaskLinePointsByIds(List<Long> lineIds) {
        return lineMapper.selectTaskLinePointsByIds(lineIds);
    }



}
